<?php

/*
* File reviews.php for project Rijlesplanner.nl
* Created on 1 dec 2007 at 20:07:05 by Ivar Pruijn
*/
class reviews extends modules {

	// Properties:
	protected $name = "reviews";
	protected $title = "Beoordelingen";
	protected $subtitle = "";
	protected $moduleOptions = array ();

	// Methods:

	/**
	* void __construct ()
	* This is the constructor method for this class.
	*/
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
		$this->wsclient = parent :: getWsclient();
	}

	/**
	* Object process()
	*
	* Processes the template and current module
	*
	* @param Smarty template The Smarty template to process (sets variables etc.)
	* @return Smarty The template that has been processed
	*/
	function process(& $template = null) {
		$this->template = $template;
		$this->randomBerichten();
		
		parent::loadModule("ads",$template);
				
		if (!empty ($_POST)) {
			if (!$this->auth->accessTo($this->name, null, $_POST['action'])) {
				$this->actionAccessDenied();
			} else {
				switch ($_POST['action']) {
					case "plaatsreactie" :
					$this->plaatsReactie();
					$this->template->assign('moduleTemplate', 'modules/reviews/plaatsreactie_leerling.tpl');
					break;
					case "vindreactie" :
					$this->vindReactie();
					$this->template->assign('moduleTemplate', 'modules/reviews/vindreactie_leerling.tpl');
					break;
				}
			}
		} else {
			if (!empty ($_GET['option'])) {
				if (!$this->auth->accessTo($this->name, $_GET['option'])) {
					$this->optionAccessDenied();
				} else {
					switch ($_GET['option']) {
						case "randomberichten" :
						$this->randomBerichten();
						$this->template->assign('moduleTemplate', 'modules/reviews/berichten.tpl');
						break;
						case "berichten" :
						$this->berichten(100);
						$this->template->assign('moduleTemplate', 'modules/reviews/berichten.tpl');
						break;
						case "plaatsreactie" :
						$this->reactiePlaatsen();
						$this->template->assign('moduleTemplate', 'modules/reviews/reactieplaatsen_leerling.tpl');
						break;
						case "vindreacties" :
						$this->vindReacties();
						$this->template->assign('moduleTemplate', 'modules/reviews/reactiesvinden_leerling.tpl');
						break;
						default :
						break;
					}
				}
			} else {
				if ($this->user instanceof Visitor) {
					$this->template->assign('moduleTemplate', 'modules/reviews/berichten.tpl');
				} else {
					if ($this->user instanceof Leerling) {
						$this->template->assign('moduleTemplate', 'modules/reviews/berichten.tpl');
					}
				}
			}
		}
	}

	/**
	* boolean berichten()
	*
	* Retrieves the review messages
	* Sets an error array in the template in case of errors.
	*/
	function berichten($aantal = 100) {
		
		if($this->user instanceof Visitor) {
			$leerlingId = 0;
			$rijschoolId = 0;
		} else {
			$leerlingId = $this->user->getId();
			$rijschoolId = $this->user->getRijschoolId();
		}
		
		$this->errors = array ();
		$reviewmessages = array ();
		$this->logger->log("Ophalen berichten: LeerlingId: " . $leerlingId . ", rijschoolId: " . $rijschoolId);

		try {
			$result = $this->wsclient->WS_Rijlesplanner_FindLeerlingReactie(array (
			'RijschoolId' =>  $rijschoolId,
			'LeerlingId' => $leerlingId,
			'Aantal' => $aantal
			));
			$reacties = $result->Reacties;
			$smartyPaginate = new SmartyPaginate();
			// required connect
			$smartyPaginate->connect("reacties");
			// set items per page
			$smartyPaginate->setLimit(10,"reacties");
			$smartyPaginate->setUrl($_SERVER['REQUEST_URI'],"reacties");
			$smartyPaginate->setUrlVar("next","reacties");
			$smartyPaginate->setNextText("Volgende reacties","reacties");
			$smartyPaginate->setPrevText("Vorige reacties","reacties");

			// assign your db results to the template
			$smartyPaginate->setTotal(count($reacties),"reacties");
			$this->template->assign('reacties', array_slice($reacties, $smartyPaginate->getCurrentIndex("reacties"), $smartyPaginate->getLimit("reacties")));
			$this->logger->log("Aantal reacties: ".count($reacties), PEAR_LOG_DEBUG);
			// assign {$paginate} var
			$smartyPaginate->assign($this->template,"paginate","reacties");
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_DEBUG);
			switch ($e->faultcode) {
				default :
				$errorcode = 71;
				$errormessage = 'Onbekende fout bij ophalen reviewberichten';
				break;
			}
			$this->errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}

		$this->template->assign('reviewmessages', $reviewmessages);
		$this->template->assign('errors', $this->errors);
	}

	public function randomBerichten($aantal = 5, $random = false) {
		$this->errors = array ();
		$reviewmessages = array ();

		if($this->user instanceof Visitor) {
			$leerlingId = 0;
			$rijschoolId = 0;
		} else {
			$leerlingId = $this->user->getId();
			$rijschoolId = $this->user->getRijschoolId();
		}

		$this->logger->log("Ophalen random berichten: LeerlingId: " . $leerlingId . ", rijschoolId: " . $rijschoolId);

		try {
			$result = $this->wsclient->WS_Rijlesplanner_FindLeerlingReactie(array (
			'RijschoolId' =>  $rijschoolId,
			'LeerlingId' => $leerlingId,
			'Aantal' => $aantal
			));
			$reacties = $result->Reacties;
			$this->logger->log('Reacties ' . print_r($reacties,true));
			$this->template->assign('reacties', $reacties);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_DEBUG);
			switch ($e->faultcode) {
				default :
				$errorcode = 71;
				$errormessage = 'Onbekende fout bij ophalen reviewberichten';
				break;
			}
			$this->errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('errors', $this->errors);
	}

	private function reactiePlaatsen() {

	}

	/**
	* boolean postReview()
	*
	* Store a review message from a Leerling
	* Sets an error array in the template in case of errors.
	*/
	private function plaatsReactie() {
		$plaatsenResult = false;
		$this->errors = array ();
		try {
			if (!isset ($_POST['reactie']) || empty ($_POST['reactie'])) {
				throw new ReviewException('Geen reactie ingevuld', 60);
			}

			$dateNu = new DateTime();
			$datum = $dateNu->format(DATE_ISO8601);
			$leerlingReactie = new LeerlingReactie();

			$leerlingReactie->setDatum($datum);
			$leerlingReactie->setOnderwerp($_POST['onderwerp']);
			$leerlingReactie->setReactie($_POST['reactie']);

			$result = $this->wsclient->WS_Rijlesplanner_SaveLeerlingReactie(array (
			'RijschoolId' => $this->user->getRijschoolId(),
			'SessionId' => $_SESSION['SessionId'],
			'LeerlingId' => $this->user->getId(),
			'Reactie' => $leerlingReactie
			));
			$this->logger->log("Result: " . print_r($result, true), PEAR_LOG_DEBUG);
			$plaatsenResult = $result->Success;
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
				$errorcode = 59;
				$errormessage = 'Onbekende fout bij het opslaan van de reactie ("' . $e->faultstring . '")';
				break;
			}
			$this->errors[] = array (
			'code' => $errorcode,
			'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (ReviewException $e) {
			$this->errors[] = array (
			'code' => $e->getCode(),
			'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $this->errors);
		$this->template->assign('plaatsenResult', $plaatsenResult);
	}

	private function vindReacties() {

	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		if (!empty ($_GET['option'])) {
			$subtitle = "";
			foreach ($this->moduleOptions AS $mOpt) {
				if ($mOpt['name'] == $_GET['option']) {
					$subtitle = $mOpt['title'];
				}
			}
			return $subtitle;
		} else {
			return $this->subtitle;
		}
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	* string __toString()
	*
	* Returns a String representation of this class.
	*
	* @return string A representation of this class
	*/
	public function __toString() {
		return $this->title;
	}
}
?>
